package reflex.learning02;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2019-06-07 22:39:00
 */
public class TestMethod {

    /**
     * 获取运行时类的方法
     */
    @Test
    public void test01(){
        Class clazz = Person.class;
        //getMethods 获取运行时类及其父类中所有的声明为public的方法
        Method[] methods = clazz.getMethods();
        for(Method m : methods){
            System.out.println(m + "");
        }
        //getDeclaredMethod 获取运行时类本身声明的所有的方法
        Method[] methods2 = clazz.getDeclaredMethods();
        for(Method m : methods2){
            System.out.println(m + "");
        }
    }

    /**
     * 注解 权限修饰符 返回值类型 方法名 形参列表 异常
     */
    @Test
    public void test02(){
        Class clazz = Person.class;
        Method[] methods = clazz.getDeclaredMethods();
        for(Method m : methods){
            //注解
            Annotation[] anns = m.getAnnotations();
            for(Annotation a : anns){
                System.out.println(a);
            }

            //权限修饰符
            System.out.print(Modifier.toString(m.getModifiers()) + " ");

            //返回值类型
            Class returnType = m.getReturnType();
            System.out.print(returnType.getName() + " ");

            //方法名
            System.out.print(m.getName());

            //形参列表
            System.out.print("(");
            Class[] params = m.getParameterTypes();
            for(int i = 0; i < params.length; i++){
                System.out.print(params[i].getName() + " args-" + i + " ");
            }
            System.out.println(")");

            //异常
            Class[] exceptionTypes = m.getExceptionTypes();
            for(Class e : exceptionTypes){
                System.out.print(e.getName() + " ");
            }

            System.out.println();
        }
    }

}
